import Vue from 'vue'
import { v1 } from 'uuid'

export function deepCopy(target) {
  if (typeof target == 'object') {
    const result = Array.isArray(target) ? [] : {}
    for (const key in target) {
      if (typeof target[key] == 'object') {
        result[key] = deepCopy(target[key])
      } else {
        result[key] = target[key]
      }
    }

    return result
  }

  return target
}

export function swap(arr, i, j) {
  const temp = arr[i]
  Vue.set(arr, i, arr[j])
  Vue.set(arr, j, temp)
}

export function $(selector) {
  return document.querySelector(selector)
}

const components = ['VText', 'RectShape', 'CircleShape']
export function isPreventDrop(component) {
  return !components.includes(component)
}

export function generateID() {
  return v1()
}

export function requestAnimationFrameTimeOut(callBack, delay, callBackTimer) {
  const timer = setTimeout(() => {
    callBack()
  }, delay)

  callBackTimer && callBackTimer(timer)

  return timer
}

export function cleanRequestAnimationFrameTimeOut(timer) {
  clearTimeout(timer)
}

export function bytesToSize(sizes) {
  let mYsize = sizes
  if (mYsize === 0) return `${0}B`
  if (mYsize < 0.1) {
    // 小于0.1KB转换为B
    mYsize = `${parseFloat((mYsize * 1024).toFixed(2))}B`
  } else if (mYsize > 0.1 * 1024) {
    // 大于0.1MB转换为MB
    mYsize = `${parseFloat((mYsize / 1024).toFixed(2))}MB`
  } else if (mYsize > 0.1 * 1024 * 1024) {
    // 大于0.1GB转换为GB
    mYsize = `${parseFloat((mYsize / 1024 / 1024).toFixed(2))}GB`
  } else {
    mYsize += 'KB'
  }

  return mYsize
}

export function formatSeconds(value) {
  let theTime = parseInt(value) // 秒
  let middle = 0 // 分
  let hour = 0 // 小时

  if (theTime > 60) {
    middle = parseInt(theTime / 60)
    theTime = parseInt(theTime % 60)
    if (middle > 60) {
      hour = parseInt(middle / 60)
      middle = parseInt(middle % 60)
    }
  }
  let result = `${parseInt(theTime)}秒`
  if (middle > 0) {
    result = `${parseInt(middle)}分${result}`
  }
  if (hour > 0) {
    result = `${parseInt(hour)}小时${result}`
  }
  return result
}
